package com.ets.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.table.JTableHeader;

import com.ets.User_DAO.User;
import com.ets.Vehicle_DAO.Vehicle;
import com.ets.Vehicle_DAO.VehicleDB;


public class VehiclePanel extends JPanel implements ActionListener
{
	private static final long serialVersionUID = 1;	

    private static VehicleDB          vehicleDAO = null;	
    private static ArrayList<Vehicle> vehicles;	
	
	private static JButton addButton,
	changeButton,
	deleteButton,
	exitButton;

	public VehiclePanel()
	{
		// display panel
		setLayout(new GridBagLayout());

		Border loweredBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED);

		JPanel vendorDisplayPanel = new JPanel();

		vendorDisplayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

		vehicleDAO = new VehicleDB();
 		vehicles = new ArrayList<Vehicle>();
 		boolean flag = vehicleDAO.getVehicles(vehicles);
 		System.out.println("Flag" + flag);

		Object[][] tempData = new Object[100][10];
		Object[][] data = tempData;

		String col[] = {"Stock Number", "VIN", "Year", "Make", "Model", "Description"};

			  int m = 0;
				
			  for (Vehicle aa : vehicles)
			    {
//				 System.out.println(aa.getNmFst());
				 tempData[m][0] = aa.getStckNo();
				 tempData[m][1] = aa.getVIN();
				 tempData[m][2] = aa.getYr();
				 tempData[m][3] = aa.getVndr();
				 tempData[m][4] = aa.getMdl();
				 tempData[m][5] = aa.getDescr();
					
				 System.out.println(tempData[m][0] + " " + tempData[m][1] + " " + tempData[m][2] + " " + tempData[m][3]);
					
				 m++;
			    }

		JTable table = new JTable(data,col);

			  table.getColumnModel().getColumn(1).setPreferredWidth(200); 
			  table.getColumnModel().getColumn(2).setPreferredWidth(200); 
			  table.getColumnModel().getColumn(3).setPreferredWidth(200); 
			  table.getColumnModel().getColumn(4).setPreferredWidth(100); 
			  table.getColumnModel().getColumn(5).setPreferredWidth(100); 

			  
		table.setGridColor(Color.BLACK);		
		JTableHeader header = table.getTableHeader();
		header.setBackground(Color.GRAY);
		table.setBackground(Color.white);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		
/*
		header.setBackground(Color.CYAN);
		table.setCellSelectionEnabled(false);
		table.setAutoscrolls(true);
		table.setGridColor(Color.BLACK);
		table.setRowSelectionAllowed(true);
*/
		//	  table.setBackground(Color.white);
		//	  table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		JScrollPane pane = new JScrollPane(table);

		//	  table.setBackground(getForeground());


		pane.setPreferredSize(new Dimension(800, 500)); 
		vendorDisplayPanel.add(pane);

		// button panel
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

		//Add button
		addButton = new JButton("Add Vehicle");
		addButton.addActionListener(this);
		buttonPanel.add(addButton);
		addButton.setToolTipText("This is the add button");

		//Change button
		changeButton = new JButton("Edit Vehicle");
		changeButton.addActionListener(this);
		buttonPanel.add(changeButton);
		changeButton.setToolTipText("This is the change button");

		// Delete button
		deleteButton = new JButton("Delete Vehicle");
		deleteButton.addActionListener(this);
		buttonPanel.add(deleteButton);
		deleteButton.setToolTipText("This is the change button");

		//Exit button
		exitButton = new JButton("Exit");
		exitButton.addActionListener(this);
		buttonPanel.add(exitButton);
		exitButton.setToolTipText("This is the exit button");

		this.add(vendorDisplayPanel, getConstraints(1,1,3,1, GridBagConstraints.CENTER));

		this.add(buttonPanel, getConstraints(1,2,3,1, GridBagConstraints.CENTER));
	}

//	private void setResizable(boolean b)
//	{
//		// TODO Auto-generated method stub
//	}

	private GridBagConstraints getConstraints (int gridx, int gridy, int gridwidth, int gridheight, int anchor)
	{
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(5, 5, 5, 5);
		c.ipadx = 5;
		c.gridx = gridx;
		c.gridy = gridy;
		c.gridwidth = gridwidth;
		c.gridheight = gridheight;
		c.anchor = anchor;
		return c;
	}

	@Override
	public void actionPerformed (ActionEvent e) 
	{
		Object source = e.getSource();
		if (source == exitButton)
			System.exit(0);
		else if (source == addButton)
			//	{
			System.out.println("Add button clicked");
		else if (source == changeButton)
			System.out.println("Change button clicked");
		else if (source == deleteButton)
			System.out.println("Delete button clicked");
		//  }

	}
}
